package com.right.health.app.ui.medical;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.magic.basic.activity.BaseActivity;
import com.magic.basic.utils.ToastUtil;
import com.magics.http.listener.OnHttpListener;
import com.magics.http.model.Request;
import com.magics.http.model.Response;
import com.right.health.app.R;
import com.right.health.app.app.App;
import com.right.health.app.bean.FamilyResponse;
import com.right.health.app.common.DataCommon;
import com.right.health.app.constants.BroadCastCode;
import com.right.health.app.dialog.BottomPickerDialog;
import com.right.health.app.net.SickHistoryApi;

import java.util.ArrayList;
import java.util.List;

/*
 * Created by hopeliao on 2019-10-01
 *  添加家族史
 */
public class FamilyMedicalAddActivity extends BaseActivity implements View.OnClickListener, OnHttpListener {

    private EditText mNameEt;

    private EditText mContentEt;

    private String mChooseType;

    private TextView mChooseTypeTv;

    private BottomPickerDialog<FamilyResponse.FamilyInfo> mPickerDialog;

    private List<FamilyResponse.FamilyInfo> mDataSource = new ArrayList<>();

    private SickHistoryApi mSickHistoryApi;

    @Override
    public void onCreate(Bundle bundle, String s) {
        setContentView(R.layout.family_medical_add);
    }

    @Override
    public void findView() {
        mContentEt = (EditText) findViewById(R.id.et_content);
        mNameEt = (EditText) findViewById(R.id.et_name);
        mChooseTypeTv = (TextView) findViewById(R.id.tv_choose_type);
        mChooseTypeTv.setOnClickListener(this);
        findViewById(R.id.tv_save).setOnClickListener(this);
    }

    @Override
    public void initialize() {
        mDataSource.clear();
        mDataSource.addAll(DataCommon.getFamilyRelationship());

        mSickHistoryApi = new SickHistoryApi();
        mSickHistoryApi.setListener(this);
    }

    private BottomPickerDialog getPickerDialog() {
        if (mPickerDialog == null) {
            mPickerDialog = new BottomPickerDialog<>(this);
            mPickerDialog.setChooseListener((tag, num) -> {
                dismissPickerDialog();
                FamilyResponse.FamilyInfo info = mDataSource.get(num);

                mChooseType = info.membership;
                mChooseTypeTv.setText(info.shipName);
            });
            mPickerDialog.setDataSource(mDataSource);
        }
        return mPickerDialog;
    }

    private void dismissPickerDialog() {
        if (mPickerDialog != null && mPickerDialog.isShowing()) {
            mPickerDialog.dismiss();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_choose_type:
                getPickerDialog().show();
                break;
            case R.id.tv_save:
                send2Net();
                break;
        }
    }

    private void send2Net() {
        String name = mNameEt.getText().toString().trim();
        if(TextUtils.isEmpty(name)) {
            ToastUtil.show(this, mNameEt.getHint().toString());
            return;
        }
        String chooseType = mChooseType;
        if(TextUtils.isEmpty(chooseType)) {
            ToastUtil.show(this, mChooseTypeTv.getHint().toString().trim());
            return;
        }
        String content = mContentEt.getText().toString().trim();
        if(TextUtils.isEmpty(content)) {
            ToastUtil.show(this, mContentEt.getHint().toString());
            return;
        }

        mSickHistoryApi.addFamilyHistory(App.getInstance().getCurrentMember().id, name, chooseType, content);
    }

    @Override
    public void onResponse(Request request, Response response) {
        if(response.isResponseFail()) {
            ToastUtil.show(this, response.error);
        } else {
            sendBroadcast(BroadCastCode.REFRESH_FAMILY_HISTORY);
            ToastUtil.show(this, "添加成功");
            finish();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        dismissPickerDialog();
    }
}
